package com.wolfalpha.utron.service.impl;

import android.text.TextUtils;

import com.wolfalpha.utron.app.AppContext;
import com.wolfalpha.utron.bean.Jlq;
import com.wolfalpha.utron.bean.Tlz;
import com.wolfalpha.utron.bean.UserInfo2;
import com.wolfalpha.utron.net.URLs;
import com.wolfalpha.utron.service.FriendsService;
import com.wolfalpha.utron.utils.JsonUtils;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by luodon on 15/9/12.
 */
public class FriendsServiceImpl implements FriendsService {

    @Override
    public Tlz getTlzInfo(int tlzId) {
        Map<String, String> mp = new HashMap<>();
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        mp.put("obj", "tlz");
        mp.put("obj_id", tlzId + "");
        String result = AppContext.getApiClient().sendPostResquest(URLs.FRIENDS_URL + "getGroupInfo", mp);
        if (result == null || JsonUtils.getData(result) == null) {
            return null;
        }
        System.out.println(result);
        return JsonUtils.paraseJson(JsonUtils.getData(result), Tlz.class);
    }

    @Override
    public Jlq getJlqInfo(int jlqId) {
        Map<String, String> mp = new HashMap<>();
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        mp.put("obj", "jlq");
        mp.put("obj_id", jlqId + "");
        String result = AppContext.getApiClient().sendPostResquest(URLs.FRIENDS_URL + "getGroupInfo", mp);
        if (result == null || JsonUtils.getData(result) == null) {
            return null;
        }
        System.out.println(result);
        return JsonUtils.paraseJson(JsonUtils.getData(result), Jlq.class);
    }

    @Override
    public List<UserInfo2> searchUser(String key) {
        List<UserInfo2> ideas = new ArrayList<UserInfo2>();
        Map<String, String> mp = new HashMap<>();
        mp.put("key", key);
        String result = AppContext.getApiClient().sendPostResquest(URLs.FRIENDS_URL + "searchUser", mp);
        if (result == null) {
            return null;
        }
        System.out.println(result);
        if (TextUtils.isEmpty(JsonUtils.getData(result))) {
            return null;
        }
        JSONArray arr = null;
        try {
            arr = new JSONArray(JsonUtils.getData(result));
            for (int i = 0; i < arr.length(); i++) {
                UserInfo2 idea = JsonUtils.paraseJson(arr.get(i).toString(), UserInfo2.class);
                ideas.add(idea);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return ideas;
    }

    @Override
    public List<Jlq> searchJlq(String key) {
        List<Jlq> ideas = new ArrayList<Jlq>();
        Map<String, String> mp = new HashMap<>();
        mp.put("key", key);
        String result = AppContext.getApiClient().sendPostResquest(URLs.FRIENDS_URL + "searchJlq", mp);
        if (result == null) {
            return null;
        }
        System.out.println(result);
        if (TextUtils.isEmpty(JsonUtils.getData(result))) {
            return null;
        }
        JSONArray arr = null;
        try {
            arr = new JSONArray(JsonUtils.getData(result));
            for (int i = 0; i < arr.length(); i++) {
                Jlq idea = JsonUtils.paraseJson(arr.get(i).toString(), Jlq.class);
                ideas.add(idea);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return ideas;
    }

    @Override
    public boolean createJlq(String name, String intro, int pId, int maxMem, String hxId) {
        Map<String, String> mp = new HashMap<>();
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        mp.put("name", name);
        mp.put("project_id", pId + "");
        mp.put("intro", intro);
        mp.put("hx_id", hxId);
        mp.put("max", maxMem + "");
        String result = AppContext.getApiClient().sendPostResquest(URLs.FRIENDS_URL + "createJlq", mp);
        if (result == null || JsonUtils.getData(result) == null) {
            return false;
        }
        System.out.println(result);
        return JsonUtils.getRet(result) == 200;
    }

    @Override
    public boolean createTlz(String name, String intro, int ideaId, String hxId) {
        Map<String, String> mp = new HashMap<>();
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        mp.put("name", name);
        mp.put("idea_id", ideaId + "");
        mp.put("hx_id", hxId);
        mp.put("intro", intro);
        String result = AppContext.getApiClient().sendPostResquest(URLs.FRIENDS_URL + "createTlz", mp);
        if (result == null || JsonUtils.getData(result) == null) {
            return false;
        }
        System.out.println(result);
        return JsonUtils.getRet(result) == 200;
    }

    @Override
    public boolean leaveGroup(int toId, boolean isQun) {
        Map<String, String> mp = new HashMap<>();
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        mp.put("obj", isQun ? "jlq" : "tlz");
        mp.put("t_id", toId + "");
        String result = AppContext.getApiClient().sendPostResquest(URLs.FRIENDS_URL + "leaveGroup", mp);
        if (result == null || JsonUtils.getData(result) == null) {
            return false;
        }
        System.out.println(result);
        return JsonUtils.getRet(result) == 200;
    }

    @Override
    public boolean enterGroup(int obId, boolean isQun) {
        Map<String, String> mp = new HashMap<>();
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        mp.put("obj", isQun ? "jlq" : "tlz");
        mp.put("obj_id", obId + "");
        String result = AppContext.getApiClient().sendPostResquest(URLs.FRIENDS_URL + "enterGroup", mp);
        if (result == null || JsonUtils.getData(result) == null) {
            return false;
        }
        System.out.println(result);
        return JsonUtils.getRet(result) == 200;
    }

    @Override
    public boolean deleteGroup(int obId, boolean isQun) {
        Map<String, String> mp = new HashMap<>();
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        mp.put("password", AppContext.getInstance().getProperty("utron_pwd"));
        mp.put("obj", isQun ? "jlq" : "tlz");
        mp.put("t_id", obId + "");
        String result = AppContext.getApiClient().sendPostResquest(URLs.FRIENDS_URL + "deleteGroup", mp);
        if (result == null || JsonUtils.getData(result) == null) {
            return false;
        }
        System.out.println(result);
        return JsonUtils.getRet(result) == 200;
    }


}
